// 防抖延迟
const delay = (function() {
	let timer = 0
	return function(callback, ms) {
		clearTimeout(timer)
		timer = setTimeout(callback, ms)
	}
})()
/**
 * 设置没有页面提交时的公共参数
 */
const setPublicInfoInfo = function(reqSubject, startTime, hopeTime, reqDes) {
	var temp = {
		reqSubject: reqSubject == undefined ? "" : reqSubject,
		startTime: startTime == undefined ? 0 : startTime,
		hopeTime: hopeTime == undefined ? 0 : hopeTime,
		reqDes: reqDes == undefined ? "" : reqDes,
	}
	return temp

}
/**
 * 计算时间差
 * @param {*} data  number
 */
const times = function(data) {
	var days = 0
	var hours = 0
	var minutes = 0

	var date2 = new Date(); //结束时间
	var date3 = date2.getTime() - data; //时间差的毫秒数
	//计算出相差天数
	days = Math.floor(date3 / (24 * 3600 * 1000))

	//计算出小时数
	var leave1 = date3 % (24 * 3600 * 1000) //计算天数后剩余的毫秒数
	hours = Math.floor(leave1 / (3600 * 1000))
	//计算相差分钟数
	var leave2 = leave1 % (3600 * 1000) //计算小时数后剩余的毫秒数
	minutes = Math.floor(leave2 / (60 * 1000))

	var temp = ""
	if (days != 0 || days != "") {
		temp += days + "天"
	}
	if (hours != 0 || hours != "") {
		temp += hours + "小时"
	}
	if (minutes != 0 || minutes != "") {
		temp += minutes + "分钟"
	}

	if (temp == "") {
		temp = "刚刚"
	}
	return temp
}
/**
 * 时间格式转换
 * @param {*} date
 * @param {*} fmt 格式 yyyy-MM-dd hh:mm:ss
 */
const formatDate = function(date, fmt) {
	if (/(y+)/.test(fmt)) {
		fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
	}
	let o = {
		'M+': date.getMonth() + 1,
		'd+': date.getDate(),
		'h+': date.getHours(),
		'm+': date.getMinutes(),
		's+': date.getSeconds()
	}
	for (let k in o) {
		if (new RegExp(`(${k})`).test(fmt)) {
			let str = o[k] + ''
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str))
		}
	}
	return fmt
}
const padLeftZero = function(str) {
	return ('00' + str).substr(str.length)
}
/**
 * 判断对象是否为空
 * @param {Object} object
 * 不为空，返回false
 */
const objectIsEmpty = function(object) {
	if (object != undefined && object != null && object != '') {
		return false
	} else {
		return true
	}
}
/**
 * 判断list是否为空
 * @param {Object} object
 * 不为空，返回false
 */
const listIsEmpty = function(list) {
	if (list != undefined && list != null && list instanceof Array && list.length > 0) {
		return false
	} else {
		return true
	}
}
/**
 * 升序
 * @param {Object} dataList 排序集合
 * @param {Object} sortValue  排序对象
 */
const sortDataByAsc = function(dataList, sortValue) {
	if (!listIsEmpty(dataList)) {
		var temp = []
		temp = dataList.sort((a, b) => {
			return a[sortValue].localeCompare(b[sortValue])
		})
		return temp
	} else {
		return dataList
	}
}
/**
 * 降序
 * @param {Object} dataList 排序集合
 * @param {Object} sortValue  排序对象
 */
const sortDataByDesc = function(dataList, sortValue) {
	if (!listIsEmpty(dataList)) {
		var temp = []
		temp = dataList.sort((a, b) => {
			return b[sortValue].localeCompare(a[sortValue])
		})
		return temp
	} else {
		return dataList
	}
}
/**
 * 去重
 * 可对简单数组去重, 如：[1,2,3,1]
 * 也可对对象性数据集合去重
 * @param {Object} list 必填，去重集合
 * @param {Object} value 选填, 根据数组中元素的某个key值去重
 */
const unique = function(list, value) {
	if (!listIsEmpty(list)) {
		if (!objectIsEmpty(value)) {
			const res = new Map();
			return list.filter((list) => !res.has(list[value]) && res.set(list[value], 1));
		} else {
			const res = new Map();
			return list.filter((list) => !res.has(list) && res.set(list, 1));
		}
	} else {
		return list
	}
}
/**
 * 获取UUID
 */
const getUUID = function(){
  var uuidA = ""
  var s = [];
  var hexDigits = '0123456789abcdef';
  for (var i = 0; i < 36; i++) {
      s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  }
  s[14] = '4';
  s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);
  s[8] = s[13] = s[18] = s[23] = '-';

  uuidA = s.join('');
  console.log(s.join(''), 's.join("")');
  return uuidA;
}

const changeUrl = [
	{
		type: "knowledge",
		url: 'https://bigdata.10086.cn/wutong/knowledge/#', // 正式
    // url: 'http://localhost:8080/#'  //测试
    // url: 'http://192.168.3.108:7077/wutong/knowledge/#'  //测试
	},
	{
		type: 2,  //门户
		url: 'https://bigdata.10086.cn/wutong/#',  // 正式
    // url: 'http://10.252.16.39:8088/wutong/#'
    // url: 'http://192.168.3.108:7077/wutong/#'      //测试
	},
	{
		type: 3,    //工单
		url: 'https://bigdata.10086.cn/wutong/support/#',  // 正式
    // url: 'http://192.168.3.108:7077/wutong/support/#'      //测试
	},
]
export default {
	delay: delay,
	setPublicInfoInfo: setPublicInfoInfo,
	formatDate: formatDate,
	times: times,
	objectIsEmpty: objectIsEmpty,
	listIsEmpty: listIsEmpty,
	unique: unique,
	changeUrl: changeUrl,
  getUUID: getUUID
}
